﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;

namespace DuJianliang.ApplicationPlatform.Reimb
{
    public partial class ReimbEdit : System.Web.UI.Page
    {
        private string FlowInstanceID
        {
            get 
            {
                if (ViewState["FlowInstanceID"] != null)
                {
                    return ViewState["FlowInstanceID"].ToString();
                }
                else
                {
                    string flowInstanceID = Guid.NewGuid().ToString();
                    ViewState["FlowInstanceID"] = flowInstanceID;
                    return flowInstanceID;
                }
            }
            set
            {
                ViewState["FlowInstanceID"] = value;
            }
        }

        private string Source
        {
            get
            {
                object obj = ViewState["Source"];
                return obj != null ? obj.ToString() : string.Empty;
            }
            set
            {
                ViewState["Source"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.FlowInstanceID = Request.QueryString["FlowInstanceID"];
            this.Source = Request.QueryString["Source"];

            if (!Page.IsPostBack)
            {
                InitControl(this.FlowInstanceID);
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            ReimbOB reimb = new ReimbOB();
            reimb.FlowInstanceID = this.FlowInstanceID;
            reimb.Dept = txtDept.Text.Trim();
            reimb.ReimbName = txtReimbName.Text.Trim();
            reimb.Currency = ddlCurrency.SelectedValue;
            reimb.Amount = txtAmount.Text.Trim();
            reimb.Purpose = txtPurpose.Text.Trim();
            string datetime = DateTime.Now.ToString();
            reimb.CreateDate = datetime;
            reimb.ModifyDate = datetime;
            reimb.CreateUser = Common.CurrentInfo.Instance.CurrentUser.UserName;
            reimb.ModifyUser = Common.CurrentInfo.Instance.CurrentUser.UserName;

            try
            {
                ReimbOB reimbOB = this.Select(this.FlowInstanceID);

                if (reimbOB != null)
                {
                    Update(reimb);
                }
                else
                {
                    
                    Insert(reimb);
                }                
                Response.Redirect("ReimbList.aspx");
            }
            catch (System.Threading.ThreadAbortException)
            {
                // do nothing. 
                // Helper.ShowMessagePage 方法中使用了 Response.Redirect，该方法会引发 ThreadAbortException 异常
                // 参考http://support.microsoft.com/kb/312629/
            }
            catch (Exception ex)
            {
                Helper.Instance.SetException(ex);
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("ReimbList.aspx");
        }

        private void InitControl(string flowInstanceID)
        {
            try
            {
                ReimbOB reimbOB = this.Select(flowInstanceID);

                if (reimbOB != null)
                {
                    txtDept.Text = reimbOB.Dept;
                    txtReimbName.Text = reimbOB.ReimbName;

                    foreach (ListItem item in ddlCurrency.Items)
                    {
                        item.Selected = false;
                    }
                    ddlCurrency.Items.FindByValue(reimbOB.Currency).Selected = true;
                    
                    txtAmount.Text = reimbOB.Amount;
                    txtPurpose.Text = reimbOB.Purpose;
                }

                //txtDept.Enabled = false;
                //txtReimbName.Enabled = false;
                //ddlCurrency.Enabled = false;
                //txtAmount.Enabled = false;
                //txtPurpose.Enabled = false;
                //btnOK.Visible = false;
                //btnCancel.Visible = false;

                //if (this.Source.ToLower().Equals("list"))
                //{
                //    if (!FlowHelper.HasFlowData(new Guid(this.FlowInstanceID)))
                //    {
                //        txtDept.Enabled = true;
                //        txtReimbName.Enabled = true;
                //        ddlCurrency.Enabled = true;
                //        txtAmount.Enabled = true;
                //        txtPurpose.Enabled = true;
                //        btnOK.Visible = true;
                //    }

                //    btnCancel.Visible = true;
                //}



            

                if (this.Source.ToLower().Equals("list"))
                {
             
                    txtDept.Enabled = true;
                    txtReimbName.Enabled = true;
                    ddlCurrency.Enabled = true;
                    txtAmount.Enabled = true;
                    txtPurpose.Enabled = true;
                    btnOK.Visible = true;
                    btnCancel.Visible = true;
                }
                else
                {
                    txtDept.Enabled = false;
                    txtReimbName.Enabled = false;
                    ddlCurrency.Enabled = false;
                    txtAmount.Enabled = false;
                    txtPurpose.Enabled = false;
                    btnOK.Visible = false;
                    btnCancel.Visible = false;
                }



          
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        private void Insert(ReimbOB reimbOB)
        {
            string sql = @"INSERT INTO [dbo].[Reimb]
                                   ([FlowInstanceID]
                                   ,[ReimbName]
                                   ,[Dept]
                                   ,[Currency]
                                   ,[Amount]
                                   ,[Purpose]
                                   ,[CreateUser]
                                   ,[CreateDate]
                                   ,[ModifyUser]
                                   ,[ModifyDate])
                             VALUES
                                   (@FlowInstanceID
                                   ,@ReimbName
                                   ,@Dept
                                   ,@Currency
                                   ,@Amount
                                   ,@Purpose
                                   ,@CreateUser
                                   ,@CreateDate
                                   ,@ModifyUser
                                   ,@ModifyDate)";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@FlowInstanceID", DbType.String, reimbOB.FlowInstanceID);
                database.AddInParameter(dbCommand, "@ReimbName", DbType.String, reimbOB.ReimbName);
                database.AddInParameter(dbCommand, "@Dept", DbType.String, reimbOB.Dept);
                database.AddInParameter(dbCommand, "@Currency", DbType.String, reimbOB.Currency);
                database.AddInParameter(dbCommand, "@Amount", DbType.String, reimbOB.Amount);
                database.AddInParameter(dbCommand, "@Purpose", DbType.String, reimbOB.Purpose);
                database.AddInParameter(dbCommand, "@CreateUser", DbType.String, reimbOB.CreateUser);
                database.AddInParameter(dbCommand, "@CreateDate", DbType.String, reimbOB.CreateDate);
                database.AddInParameter(dbCommand, "@ModifyUser", DbType.String, reimbOB.ModifyUser);
                database.AddInParameter(dbCommand, "@ModifyDate", DbType.String, reimbOB.ModifyDate);
                database.ExecuteNonQuery(dbCommand);                
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private ReimbOB Select(string flowInstanceID)
        {
            string sql = @"SELECT * FROM dbo.Reimb WHERE FlowInstanceID = @FlowInstanceID";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@FlowInstanceID", DbType.String, flowInstanceID);
                IDataReader reader = database.ExecuteReader(dbCommand);
                ReimbOB reimbOB = null;
                if (reader.Read())
                {
                    reimbOB = new ReimbOB();
                    reimbOB.FlowInstanceID = reader["FlowInstanceID"] != null ? reader["FlowInstanceID"].ToString() : string.Empty;
                    reimbOB.ReimbName = reader["ReimbName"] != null ? reader["ReimbName"].ToString() : string.Empty;
                    reimbOB.Dept = reader["Dept"] != null ? reader["Dept"].ToString() : string.Empty;
                    reimbOB.Currency = reader["Currency"] != null ? reader["Currency"].ToString() : string.Empty;
                    reimbOB.Amount = reader["Amount"] != null ? reader["Amount"].ToString() : string.Empty;
                    reimbOB.Purpose = reader["Purpose"] != null ? reader["Purpose"].ToString() : string.Empty;
                    reimbOB.CreateUser = reader["CreateUser"] != null ? reader["CreateUser"].ToString() : string.Empty;
                    reimbOB.CreateDate = reader["CreateDate"] != null ? reader["CreateDate"].ToString() : string.Empty;
                    reimbOB.ModifyUser = reader["ModifyUser"] != null ? reader["ModifyUser"].ToString() : string.Empty;
                    reimbOB.ModifyDate = reader["ModifyDate"] != null ? reader["ModifyDate"].ToString() : string.Empty;

                    reader.Close();
                }

                return reimbOB;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void Update(ReimbOB reimbOB)
        {
            string sql = @"UPDATE [dbo].[Reimb]
                               SET [ReimbName] = @ReimbName
                                  ,[Dept] = @Dept
                                  ,[Currency] = @Currency
                                  ,[Amount] = @Amount
                                  ,[Purpose] = @Purpose
                                  ,[ModifyUser] = @ModifyUser
                                  ,[ModifyDate] = @ModifyDate
                             WHERE [FlowInstanceID] = @FlowInstanceID";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@FlowInstanceID", DbType.String, reimbOB.FlowInstanceID);
                database.AddInParameter(dbCommand, "@ReimbName", DbType.String, reimbOB.ReimbName);
                database.AddInParameter(dbCommand, "@Dept", DbType.String, reimbOB.Dept);
                database.AddInParameter(dbCommand, "@Currency", DbType.String, reimbOB.Currency);
                database.AddInParameter(dbCommand, "@Amount", DbType.String, reimbOB.Amount);
                database.AddInParameter(dbCommand, "@Purpose", DbType.String, reimbOB.Purpose);
                database.AddInParameter(dbCommand, "@ModifyUser", DbType.String, reimbOB.ModifyUser);
                database.AddInParameter(dbCommand, "@ModifyDate", DbType.String, reimbOB.ModifyDate);
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }        
    }
}